﻿/*
 *  WMI_CRYPT - 2015(c) Nguyen hoang Tung
 *  http://tungpc.com - admin@tungpc.com
 *  
 *  Basic encrypt function 
 *  Base64 En/Decode
 *  ECB En/Decode
 */

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;

namespace WMI_PHP_OBFS
{
    public class WMI_Crypt
    {
        public string Base64Encode(string plainText)
        {
            var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText);
            return System.Convert.ToBase64String(plainTextBytes);
        }

        public string Base64Decode(string base64EncodedData)
        {
            var base64EncodedBytes = System.Convert.FromBase64String(base64EncodedData);
            return System.Text.Encoding.UTF8.GetString(base64EncodedBytes);
        }

        public string ECB_Decrypt(string encryptedString, string Key, string IV)
        {
            return ECB.ECB_Decrypt(encryptedString, Key, IV);
        }

        public string ECB_Encrypt(string plaintext,string key,string IV)
        {
           return ECB.ECB_Encrypt(plaintext, key, IV);
        }

    }

    public static class ECB
    {
        public static string ECB_Encrypt(string message, string Key, string IV){
            Encoding byteEncoder = Encoding.UTF8;
            byte[] rijnKey = byteEncoder.GetBytes(Key);
            byte[] rijnIV = byteEncoder.GetBytes(IV);

            return EncryptIt(message, rijnKey, rijnIV);
        }

        public static string ECB_Decrypt(string encrypt, string Key, string IV)
        {
            Encoding byteEncoder = Encoding.UTF8;
            byte[] rijnKey = byteEncoder.GetBytes(Key);
            byte[] rijnIV = byteEncoder.GetBytes(IV);

            return DecryptIt(encrypt, rijnKey, rijnIV);
        }

        public static String EncryptIt(String s, byte[] key, byte[] IV)
        {
            String result;
            RijndaelManaged rijn = new RijndaelManaged();
            rijn.Mode = CipherMode.ECB;
            rijn.Padding = PaddingMode.Zeros;
            using (MemoryStream msEncrypt = new MemoryStream())
            {
                using (ICryptoTransform encryptor = rijn.CreateEncryptor(key, IV))
                {
                    using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                    {
                        using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                        {


                            swEncrypt.Write(s);
                        }
                    }
                }
                result = Convert.ToBase64String(msEncrypt.ToArray());
            }
            rijn.Clear();
            return result;
        }
        public static String DecryptIt(String s, byte[] key, byte[] IV)
        {
            String result;
            RijndaelManaged rijn = new RijndaelManaged();
            rijn.Mode = CipherMode.ECB;
            rijn.Padding = PaddingMode.Zeros;
            using (MemoryStream msDecrypt = new MemoryStream(Convert.FromBase64String(s)))
            {
                using (ICryptoTransform decryptor = rijn.CreateDecryptor(key, IV))
                {
                    using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                    {
                        using (StreamReader swDecrypt = new StreamReader(csDecrypt))
                        {
                            result = swDecrypt.ReadToEnd();
                        }
                    }
                }
            }
            rijn.Clear();
            return result;
        }
    }

}
